package com.inspur.dgov.vlbod.controller;

import com.alibaba.fastjson.JSONObject;
import com.inspur.dgov.vlbod.constant.ServiceConstant;
import com.inspur.dgov.vlbod.entity.LiveShare;
import com.inspur.dgov.vlbod.http.DgovResponse;
import com.inspur.dgov.vlbod.service.LiveShareService;
import com.inspur.dgov.vlbod.service.UserRecordService;
import com.inspur.dgov.vlbod.util.CTools;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/share")
public class LiveShareController {

    private LiveShareService liveShareService;

    private UserRecordService userRecordService;

    public LiveShareController(LiveShareService liveShareService, UserRecordService userRecordService){
        this.liveShareService = liveShareService;
        this.userRecordService = userRecordService;
    }

    @PostMapping("/insert/log")
    public DgovResponse insertLog(HttpServletRequest request, @RequestBody JSONObject json){
        String broadcastId = json.getString("broadcastId");
        String liveId = json.getString("liveId");
        String meetingRoomNumber = json.getString("meetingRoomNumber");
        try{
            String userId = this.userRecordService.getUserRecord(request).getUserId();
            //记录分享表
            LiveShare liveShare = new LiveShare();
            liveShare.setShareUrl(CTools.getSnowflakeId());
            liveShare.setBroadcastId(broadcastId);
            liveShare.setLiveId(liveId);
            liveShare.setMeetingRoomNumber(meetingRoomNumber);
            liveShare.setUserId(userId);
            liveShare.setCreateTime(LocalDateTime.now());
            liveShare.setStatus(ServiceConstant.SYSTEM_ON);
            this.liveShareService.save(liveShare);
            return DgovResponse.success("");
        }catch (Exception e){
            log.error("share insert log error:" + e.getMessage());
            return DgovResponse.fail("记录分享日志失败");
        }
    }
}
